﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using yiCommerce.Core;
using yiCommerce.Core.Data;
using yiCommerce.Core.Domain.Customers;
using yiCommerce.Core.Domain.FriendShips;
using yiCommerce.Core.Domain.Pictures;
using yiCommerce.Core.Domain.Products;
using yiCommerce.Core.Domain.Statuses;
using yiCommerce.Service.Orders;

namespace yiCommerce.Service.Statuses
{
    public class StatusService:IStatusService
    {
        private IRepository<Status> _statusRep;
        private IRepository<FriendShip> _friendShipRep;

        public StatusService(IRepository<Status> statusRep, IRepository<FriendShip> friendShipRep)
        {
            _statusRep = statusRep;
            _friendShipRep = friendShipRep;
        }

        


        public Status Update(string text, int customerId, IList<int> pictureIds, bool isProudct, int? productId )
        {

            var status = new Status()
                {
                    CreatedAt = DateTime.Now,
                    CustomerId = customerId,
                    Deleted = false,
                    Text = text,
                    IsProductStatus = isProudct
                    //StatusPictures = new Collection<StatusPicture>()
                    //    {
                            
                    //    }
                };

            if (isProudct)
            {
                status.ProductId = productId.Value;
            }

            _statusRep.Insert(status);


            if (pictureIds != null)
            {
                
                status.StatusPictures = pictureIds.Select((picId, index) => new StatusPicture()
                {
                    DisplayOrder = index + 1,
                    PictureId = picId,
                    StatusId = status.Id,
                }).ToList();

                _statusRep.Update(status);
            }

            return this.GetById(status.Id);
        }

        private Status GetById(int id)
        {
            var query = from s in _statusRep.Table
                        where s.Id ==id 
                              && s.Deleted == false
                        select s;
            var p = query.SingleOrDefault();
            if (p != null)
            {
                return StatusMapper.ToStatusModel(p);
            }
            else
            {
                return null;
            }
        }

        public IList<Status> UserTimeLine(int customerId, int pageIndex, int pageSize)
        {
            var query = from s in _statusRep.Table
                        where s.CustomerId == customerId
                              && s.Deleted == false
                              orderby s.CreatedAt descending 
                        select s;
            var tempResult = new PagedList<Status>(query, pageIndex, pageSize);
             return StatusMapper.ToStatusesList(tempResult.ToList());
           
        }

        public IList<Status> HomeTimeLine(int customerId,int pageIndex, int pageSize)
        {


            var status = (from s in _statusRep.Table
                          where s.CustomerId == customerId
                                && s.Deleted == false
                          select s).Union(

                              from f in _friendShipRep.Table
                              join s in _statusRep.Table
                                  on f.FriendId equals s.CustomerId
                              where f.OwnerId == customerId && s.Deleted == false
                              select s);



             status =  status.OrderByDescending(c => c.CreatedAt);


            var tempResult = new PagedList<Status>(status, pageIndex, pageSize);

            return StatusMapper.ToStatusesList(tempResult.ToList());
            
        }

        public Status GetStatusById(int statusId)
        {
            var statusQuery = from s in _statusRep.Table
                         where s.Id == statusId
                         select s;
            var statuses = statusQuery.ToList();

            if (statuses != null)
            {

                var status= statuses[0];
                return StatusMapper.ToStatusModel(status);
            }
            else
            {
                return null;
            }
        }
    }
}